

package kr.ac.ssu.yoobh17.mPlayer.customView;


import kr.ac.ssu.yoobh17.mPlayer.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;


public class PlayPauseButton extends FrameLayout implements OnClickListener {
	
	private Button button_playPause;
	
	private boolean isPlaying = false;

	//---------------------------------------------------------------------------------------------------

	private OnPlayPauseButtonClickListner playPauseButtonClickListner = null;
	
	
	public static interface OnPlayPauseButtonClickListner{
		
		public void onPlayPauseButtonClick( boolean isPlaying );
		
	}

	public void setOnPlayPauseButtonClickListner( OnPlayPauseButtonClickListner playPauseButtonClickListner ){

		this.playPauseButtonClickListner = (OnPlayPauseButtonClickListner) playPauseButtonClickListner;

	}

	//---------------------------------------------------------------------------------------------------
	
	public PlayPauseButton( Context context ) {

		this( context, null );
		// TODO Auto-generated constructor stub
	}

	public PlayPauseButton( Context context, AttributeSet attrs ) {

		super( context, attrs );
		// TODO Auto-generated constructor stub
		
		initView( context );
		
	}

	private void initView( Context context ) {

		// TODO Auto-generated method stub
		
		LayoutParams layoutParams = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );
		
		button_playPause = new Button( context );
		button_playPause.setLayoutParams( layoutParams );
		button_playPause.setBackgroundResource( R.drawable.button_play );

		button_playPause.setOnClickListener( this );
		
		addView( button_playPause );
		
		setState( isPlaying );
		
	}

	@Override
	public void onClick( View v ) {

		// TODO Auto-generated method stub
		
		toggleButton();

		if( null != playPauseButtonClickListner ){
			
			playPauseButtonClickListner.onPlayPauseButtonClick( isPlaying );
			
		}

	}

	//---------------------------------------------------------------------------------------------------
	
	private void toggleButton(){

		if( isPlaying ){
			
			button_playPause.setBackgroundResource( R.drawable.button_pause );
			
			isPlaying = false;
			
		}
		else{

			button_playPause.setBackgroundResource( R.drawable.button_play );

			isPlaying = true;
			
		}
		
	}

	//---------------------------------------------------------------------------------------------------
	
	public void setState( boolean isPlaying ){
		
		this.isPlaying = isPlaying;

		if( this.isPlaying ){

			button_playPause.setBackgroundResource( R.drawable.button_pause );
			
		}
		else{

			button_playPause.setBackgroundResource( R.drawable.button_play );

		}
		
	}

	public boolean getState(){
		
		return isPlaying;
	}

}
